package com.spzx.channel.service.impl;

import com.alibaba.fastjson.JSON;
import com.spzx.channel.domain.ItemVo;
import com.spzx.channel.service.IItemService;
import com.spzx.common.core.constant.SecurityConstants;
import com.spzx.common.core.domain.R;
import com.spzx.common.core.exception.ServiceException;
import com.spzx.product.api.RemoteProductService;
import com.spzx.product.domain.*;
import io.swagger.v3.oas.annotations.media.Schema;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;

@Service
public class IItemServiceImpl implements IItemService {

    @Autowired
    private RemoteProductService remoteProductService;
    @Autowired
    private ThreadPoolExecutor threadPoolExecutor;



    //多线程优化,串行修改为并行
    public ItemVo item1(Long skuId) {

/*
        //布隆过滤器校验
        RBloomFilter<Object> bloomFilter =
                redissonClient.getBloomFilter("sku:bloom:filter");
        if(!bloomFilter.contains(skuId)) {
            throw new ServiceException("用户查询商品sku不存在");
        }
*/


        ItemVo itemVo = new ItemVo();


        //创建子线程进行远程调用

        CompletableFuture<ProductSku> productSkuCompletableFuture = CompletableFuture.supplyAsync(() -> {
            //1 根据skuId获取sku信息
            R<ProductSku> productSkuResult = remoteProductService.getProductSku(skuId,
                    SecurityConstants.INNER);
            ProductSku productSku = productSkuResult.getData();
            //设置到vo里面
            itemVo.setProductSku(productSku);
            return productSku;
        }, threadPoolExecutor);



        //因为商品信息,获取商品id,需要从productsku中获取到
        CompletableFuture<Void> productCompletableFuture = productSkuCompletableFuture.thenAcceptAsync(productSku -> {
            //2根据商品id获取商品基本信息
            R<Product> productResult =
                    remoteProductService.getProduct(productSku.getProductId(), SecurityConstants.INNER);
            Product product = productResult.getData();
            //设置到vo里面
            itemVo.setProduct(product);

            //@Schema(description = "商品轮播图列表")
            //private List<String> sliderUrlList;
            String sliderUrls = product.getSliderUrls();
            List<String> sliderUrlsList = Arrays.asList(sliderUrls.split(","));
            itemVo.setSliderUrlList(sliderUrlsList);
            itemVo.setSpecValueList(JSON.parseArray(product.getSpecValue()));

        }, threadPoolExecutor);


        //3 根据skuId获取价格
        CompletableFuture<Void> skuPriceCompletableFuture = CompletableFuture.runAsync(() -> {
            R<SkuPrice> skuPriceResult =
                    remoteProductService.getSkuPrice(skuId, SecurityConstants.INNER);
            SkuPrice skuPrice = skuPriceResult.getData();
            //设置到vo里面
            itemVo.setSkuPrice(skuPrice);
        }, threadPoolExecutor);


        //4 根据商品id获取详细信息
        //商品id,需要从第一个线程sku中获取到
        CompletableFuture<Void> productDetailsCompletableFuture = productSkuCompletableFuture.thenAcceptAsync(productSku -> {

            R<ProductDetails> productDetailsResult =
                    remoteProductService.getProductDetails(productSku.getProductId(), SecurityConstants.INNER);
            ProductDetails productDetails = productDetailsResult.getData();

            // 01.jpg,02.jpg,03.jpg
            String imageUrls = productDetails.getImageUrls();
            String[] split = imageUrls.split(",");
            List<String> list = Arrays.asList(split);
            //设置到vo里面
            itemVo.setDetailsImageUrlList(list);
        }, threadPoolExecutor);


        //5 获取商品规格对应商品skuId信息
        CompletableFuture<Void> productSpecCompletableFuture = productSkuCompletableFuture.thenAcceptAsync(productSku -> {

            R<Map<String, Long>> skuSpecValueResult =
                    remoteProductService.getSkuSpecValue(productSku.getProductId(), SecurityConstants.INNER);
            if (R.FAIL == skuSpecValueResult.getCode()) {
                throw new ServiceException(skuSpecValueResult.getMsg());
            }
            Map<String, Long> skuSpecValueMap = skuSpecValueResult.getData();
            itemVo.setSkuSpecValueMap(skuSpecValueMap);
        }, threadPoolExecutor);


        //6 获取商品库存信息

        CompletableFuture<Void> skuStockVoCompletableFuture = CompletableFuture.runAsync(() -> {
            R<SkuStockVo> skuStockResult =
                    remoteProductService.getSkuStock(skuId, SecurityConstants.INNER);
            if (R.FAIL == skuStockResult.getCode()) {
                throw new ServiceException(skuStockResult.getMsg());
            }
            SkuStockVo skuStockVo = skuStockResult.getData();
            itemVo.setSkuStockVo(skuStockVo);
        }, threadPoolExecutor);


        //7.汇总
        CompletableFuture.allOf(
                productSkuCompletableFuture,
                productCompletableFuture,
                skuPriceCompletableFuture,
                productDetailsCompletableFuture,
                productSpecCompletableFuture,
                skuStockVoCompletableFuture
        ).join();

        return itemVo;

    }



    @Override
    public ItemVo item(Long skuId) {

        ItemVo itemVo = new ItemVo();
        //1 //根据skuId获取sku信息
        R<ProductSku> productSkuResult = remoteProductService.getProductSku(skuId,
                SecurityConstants.INNER);
        ProductSku productSku = productSkuResult.getData();
        //设置到vo里面
        itemVo.setProductSku(productSku);

        //2根据商品id获取商品基本信息
        R<Product> productResult =
                remoteProductService.getProduct(productSku.getProductId(), SecurityConstants.INNER);
        Product product = productResult.getData();
        //设置到vo里面
        itemVo.setProduct(product);

        //@Schema(description = "商品轮播图列表")
        //private List<String> sliderUrlList;
        String sliderUrls = product.getSliderUrls();
        List<String> sliderUrlsList = Arrays.asList(sliderUrls.split(","));
        itemVo.setSliderUrlList(sliderUrlsList);
        itemVo.setSpecValueList(JSON.parseArray(product.getSpecValue()));

        //3 根据skuId获取价格
        R<SkuPrice> skuPriceResult =
                remoteProductService.getSkuPrice(skuId, SecurityConstants.INNER);
        SkuPrice skuPrice = skuPriceResult.getData();
        //设置到vo里面
        itemVo.setSkuPrice(skuPrice);

        //4 根据商品id获取详细信息
        R<ProductDetails> productDetailsResult = remoteProductService.getProductDetails(productSku.getProductId(), SecurityConstants.INNER);
        ProductDetails productDetails = productDetailsResult.getData();



        // 01.jpg,02.jpg,03.jpg
        String imageUrls = productDetails.getImageUrls();
        String[] split = imageUrls.split(",");
        List<String> list = Arrays.asList(split);
        //设置到vo里面
        itemVo.setDetailsImageUrlList(list);

        //5 获取商品规格对应商品skuId信息
        R<Map<String, Long>> skuSpecValueResult =
                remoteProductService.getSkuSpecValue(productSku.getProductId(), SecurityConstants.INNER);
        if (R.FAIL == skuSpecValueResult.getCode()) {
            throw new ServiceException(skuSpecValueResult.getMsg());
        }
        Map<String, Long> skuSpecValueMap = skuSpecValueResult.getData();
        itemVo.setSkuSpecValueMap(skuSpecValueMap);

        //6 获取商品库存信息
        R<SkuStockVo> skuStockResult =
                remoteProductService.getSkuStock(skuId, SecurityConstants.INNER);
        if (R.FAIL == skuStockResult.getCode()) {
            throw new ServiceException(skuStockResult.getMsg());
        }
        SkuStockVo skuStockVo = skuStockResult.getData();
        itemVo.setSkuStockVo(skuStockVo);

        //TODO 后续使用多线程完善，目前串行方式
        return itemVo;
    }
}
